// 页面加载效果管理器
const app = getApp()

class PageLoadingManager {
  constructor() {
    this.pageLoadingStates = new Map()
    this.defaultLoadingTime = 800 // 默认加载时间
    this.minLoadingTime = 300 // 最小加载时间
  }

  // 页面进入时显示加载效果
  showPageLoading(pageName, options = {}) {
    const {
      skeletonType = 'default',
      loadingText = '页面加载中...',
      loadingTime = this.defaultLoadingTime,
      customClass = ''
    } = options

    // 记录页面加载状态
    this.pageLoadingStates.set(pageName, {
      startTime: Date.now(),
      loadingTime,
      skeletonType,
      loadingText,
      customClass
    })

    // 显示全局加载
    app.showGlobalLoading({
      skeletonType,
      loadingText,
      customClass
    })

    // 设置最小加载时间
    setTimeout(() => {
      this.hidePageLoading(pageName)
    }, Math.max(loadingTime, this.minLoadingTime))
  }

  // 页面加载完成时隐藏加载效果
  hidePageLoading(pageName) {
    const loadingState = this.pageLoadingStates.get(pageName)
    if (!loadingState) return

    const elapsedTime = Date.now() - loadingState.startTime
    const remainingTime = Math.max(0, loadingState.loadingTime - elapsedTime)

    // 确保最小加载时间
    setTimeout(() => {
      app.hideGlobalLoading()
      this.pageLoadingStates.delete(pageName)
    }, remainingTime)
  }

  // 根据页面类型获取合适的骨架屏类型
  getSkeletonTypeByPage(pageName) {
    const skeletonTypeMap = {
      'pages/service/index/index': 'service',
      'pages/user/mine/index': 'list',
      'pages/village/cases/index': 'card',
      'pages/village/complaints/index': 'card',
      'pages/user/progress/index': 'list',
      'pages/user/favorites/index': 'list',
      'pages/user/activities/index': 'card',
      'pages/common/index/index': 'default',
      'pages/village/open/index': 'default'
    }

    return skeletonTypeMap[pageName] || 'default'
  }

  // 根据页面类型获取合适的加载文本
  getLoadingTextByPage(pageName) {
    const loadingTextMap = {
      'pages/service/index/index': '服务事项加载中...',
      'pages/user/mine/index': '个人信息加载中...',
      'pages/village/cases/index': '案例信息加载中...',
      'pages/village/complaints/index': '投诉信息加载中...',
      'pages/user/progress/index': '进度查询加载中...',
      'pages/user/favorites/index': '收藏列表加载中...',
      'pages/user/activities/index': '活动信息加载中...',
      'pages/common/index/index': '首页加载中...',
      'pages/village/open/index': '村务公开加载中...'
    }

    return loadingTextMap[pageName] || '页面加载中...'
  }

  // 页面进入时的自动加载效果
  onPageEnter(pageName, options = {}) {
    const skeletonType = options.skeletonType || this.getSkeletonTypeByPage(pageName)
    const loadingText = options.loadingText || this.getLoadingTextByPage(pageName)
    const loadingTime = options.loadingTime || this.defaultLoadingTime

    this.showPageLoading(pageName, {
      skeletonType,
      loadingText,
      loadingTime,
      customClass: options.customClass
    })
  }

  // 页面数据加载完成
  onPageDataLoaded(pageName) {
    this.hidePageLoading(pageName)
  }

  // 强制隐藏页面加载（用于数据请求完成时）
  forceHidePageLoading(pageName) {
    const loadingState = this.pageLoadingStates.get(pageName)
    if (loadingState) {
      // 立即隐藏加载状态
      const app = getApp()
      app.hideGlobalLoading()
      this.pageLoadingStates.delete(pageName)
      console.log(`强制隐藏页面加载: ${pageName}`)
    }
  }

  // 包装页面onLoad方法
  wrapPageOnLoad(originalOnLoad, pageName, options = {}) {
    return function(options) {
      // 显示页面加载效果
      const pageLoadingManager = require('./pageLoadingManager')
      pageLoadingManager.onPageEnter(pageName, options)
      
      // 执行原始onLoad方法
      if (originalOnLoad) {
        originalOnLoad.call(this, options)
      }
    }
  }

  // 包装页面onReady方法
  wrapPageOnReady(originalOnReady, pageName) {
    return function() {
      // 执行原始onReady方法
      if (originalOnReady) {
        originalOnReady.call(this)
      }
      
      // 页面准备完成，隐藏加载效果
      const pageLoadingManager = require('./pageLoadingManager')
      pageLoadingManager.onPageDataLoaded(pageName)
    }
  }

  // 包装页面onShow方法
  wrapPageOnShow(originalOnShow, pageName, options = {}) {
    return function() {
      // 执行原始onShow方法
      if (originalOnShow) {
        originalOnShow.call(this)
      }
      
      // 如果是从其他页面返回，显示短暂加载效果
      if (options.showOnReturn) {
        const pageLoadingManager = require('./pageLoadingManager')
        pageLoadingManager.onPageEnter(pageName, {
          loadingTime: 500,
          loadingText: '页面刷新中...'
        })
      }
    }
  }
}

// 创建全局实例
const pageLoadingManager = new PageLoadingManager()

// 导出工具函数
export const showPageLoading = (pageName, options) => pageLoadingManager.showPageLoading(pageName, options)
export const hidePageLoading = (pageName) => pageLoadingManager.hidePageLoading(pageName)
export const onPageEnter = (pageName, options) => pageLoadingManager.onPageEnter(pageName, options)
export const onPageDataLoaded = (pageName) => pageLoadingManager.onPageDataLoaded(pageName)
export const forceHidePageLoading = (pageName) => pageLoadingManager.forceHidePageLoading(pageName)
export const wrapPageOnLoad = (originalOnLoad, pageName, options) => pageLoadingManager.wrapPageOnLoad(originalOnLoad, pageName, options)
export const wrapPageOnReady = (originalOnReady, pageName) => pageLoadingManager.wrapPageOnReady(originalOnReady, pageName)
export const wrapPageOnShow = (originalOnShow, pageName, options) => pageLoadingManager.wrapPageOnShow(originalOnShow, pageName, options)

// 导出管理器实例
export default pageLoadingManager
